package com.stylefeng.guns.modular.customer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.customer.service.IVisitPicService;
import com.stylefeng.guns.modular.customer.service.IVisitService;
import com.stylefeng.guns.modular.system.dao.VisitMapper;
import com.stylefeng.guns.modular.system.model.Visit;
import com.stylefeng.guns.modular.system.model.VisitPic;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 拜访记录 服务实现类
 * </p>
 *
 * @author yinjc123
 * @since 2018-04-14
 */
@Service
public class VisitServiceImpl extends ServiceImpl<VisitMapper, Visit> implements IVisitService {

    @Autowired
    private IVisitPicService visitPicService;

    @Override
    @Transactional
    public void insert(Visit visit, String[] urls) {
        insert(visit);
        insertPic(urls,visit.getId());
    }

    @Override
    @Transactional
    public void updateById(Visit visit, String[] urls) {
        //删除旧的图片
        visitPicService.deleteByVisitId(visit.getId());
        insertPic(urls,visit.getId());
        updateById(visit);
    }

    private void insertPic(String[] urls,Integer visitId) {
        if (urls != null && urls.length > 0) {
            for (String url : urls) {
                if (StringUtils.isNotBlank(url)) {
                    VisitPic visitPic = new VisitPic(visitId, url);
                    visitPicService.insert(visitPic);
                }
            }
        }
    }

    @Override
    public void deleteByCustomerCompanyId(Integer customerCompanyId) {
        EntityWrapper<Visit> ew = new EntityWrapper<Visit>();
        ew.eq("customer_company_id", customerCompanyId);
        delete(ew);
    }
}
